// pages/home/home.js
var requests = require('../../requests/api.js');
let app = getApp()
let cache = require('../../utils/cache.js');
import util from '../../utils/util.js'

Page({

  /**
   * Page initial data
   */
  data: {
    modules: [

    ], //PageModuleVo[ ] 首页模块列表
    goods: [],
    showFloatWindow: false,
    showFavoriteModal: true,
    dialogvisible: false, //领取优惠券弹框

  },

  /**
   * Lifecycle function--Called when page load
   */
  onLoad: function(options) {
  
    new app.WeToast();
    var that = this
    that.request_indexData()
    wx.getStorage({
      key: 'FavoriteModal',
      success: function(res) {
        that.setData({
          showFavoriteModal: res.data
        })
      },
    });


  },
  // 领取成功
  showDialog: function() {
    this.setData({
      dialogvisible: true
    })
  },
  closeDialog: function() {
    this.setData({
      dialogvisible: false
    })

  },
  //优惠券领取成功返回首页按钮
  confirm: function() {

    this.closeDialog()
    wx.navigateTo({
      url: '../preferential/coupon',
    })

  },
  //优惠券领取成功去看看你按钮
  cancel: function() {
    this.closeDialog()

  },

  // 弹出层里面的弹窗
  ok: function() {
    this.setData({
      showFloatWindow: false
    })
  },
  // 顶部提示
  favorite_close: function() {
    this.setData({
      showFavoriteModal: false
    })
    wx.setStorage({
      key: 'FavoriteModal',
      data: this.data.showFavoriteModal,
    })
  },
  //banner点击事件
  bannerClick: function(event) {
    var banners = event.target.dataset.banners;
    console.log(banners)
    this.banner_link(banners)
  },
  // 判断跳转到哪个页面
  banner_link: function(e) {
    var link_type = e.bannerLinkType;

    var link_id = e.bannerLinkTargetId;
    this.couponId = link_id;
    switch (link_type) {
      case 1: //web网页
        wx.navigateTo({
          url: 'webView?url=' + e.bannerLinkUrl,
        })

        break
      case 4: //品牌详情页

        wx.navigateTo({
          url: '../brandGroup/brandGroup?brandId=' + link_id,
        })
        break
      case 5: //设计师详情页

        wx.navigateTo({
          url: '../designerGroup/designerGroup?designerId=' + link_id,
        })
        break
      case 6: //商品详情页
        wx.navigateTo({
          url: '../product/productDetail?productId=' + link_id,
        })
        break;
      case 8: //专题详情
        wx.navigateTo({
          url: '../topic/productTopic?topicId=' + link_id,
        })
        break;
      case 10: //产品组详情
        wx.navigateTo({
          url: '../topic/productGroup?groupId=' + link_id,
        })
        break;
      case 100: //领取优惠券
        this.request_coupon(link_id)

        break;
      default:
        break;
    }
  },
  //商品点击详情

  toDetailsTap: function(e) {
    var id = e.currentTarget.dataset.productid;
    wx.navigateTo({
      url: '../product/productDetail?productId=' + id,
    })
  },
  searchClickEvent: function() {
    wx.navigateTo({
      url: '../search/search',
    })
  },
  more_403_click: function(data) {
    var model = data.target.dataset.model
    wx.navigateTo({
      url: '../topic/productGroup?groupId=' + model.id,
    })
    console.log(model)
  },
  more_601_click: function(data) {
    var model = data.target.dataset.model
    wx.navigateTo({
      url: '../topic/productTopic?topicId=' + model.id,
    })
    console.log(model)
  },
  floatClick: function() {
    this.setData({
      showFloatWindow: false
    })
    wx.navigateTo({
      url: 'newCoupon',
    })
  },
  /**
   * Lifecycle function--Called when page is initially rendered
   */
  onReady: function() {
    //如果app登录了不弹，没有登录才弹窗
    console.log('onReady' + app.globalData.launch)
    if (app.globalData.launch && app.isNeedLogin()) {
      this.setData({
        showFloatWindow: true
      })
      app.globalData.launch = false
    }
  },

  /**
   * Lifecycle function--Called when page show
   */
  onShow: function() {
    //如果是新人礼券登录成功过来的就弹窗
    if (app.globalData.getCoupon && cache.endUser.firstLogin) {
      app.globalData.getCoupon = false
      this.showDialog()
    }
    //已经登录的隐藏弹窗
    if (!app.isNeedLogin()) {
      this.setData({
        showFloatWindow: false
      })
    }
  },

  /**
   * Lifecycle function--Called when page hide
   */
  onHide: function() {

  },

  /**
   * Lifecycle function--Called when page unload
   */
  onUnload: function() {

  },

  /**
   * Page event handler function--Called when user drop down
   */
  onPullDownRefresh: function() {

  },

  /**
   * Called when page reach bottom
   */
  onReachBottom: function() {

  },

  /**
   * Called when user click on the top right corner to share
   */
  onShareAppMessage: function() {

  },
  //NET 
  request_indexData: function() {
    //网络请求的时候先把加载框加出来
    wx.showLoading({
      title: '',
    })
    const self = this;
    requests.get_index((res) => {
      console.log(res)
      wx.hideLoading()
      self.setData({
        modules: res.data.modules
      })
      console.log(self.data.modules)

    }, (data) => {
      wx.hideLoading()
      if (util.alertNetworkError(data)) {
        self.wetoast.toast({
          title: data.resMsg,
        })
      }

    }, () => {
      wx.hideLoading()
    });
  },
  // 优惠券接口
  request_coupon: function(link_id) {
    if (app.toLogin()) {
      return
    }
    //网络请求的时候先把加载框加出来
    const self = this;
    var params = {
      couponId: link_id
    }
    requests.post_coupon(params, (res) => {
      console.log(res)
      this.showDialog()

    }, (data) => {
      if (util.alertNetworkError(data)) {
        self.wetoast.toast({
          title: data.resMsg,
        })
      }
    }, () => {

    });
  },
})